package com.csii.modules.online.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csii.core.system.vo.LoginUser;
import com.csii.modules.online.auth.entity.OnlAuthPage;
import com.csii.modules.online.auth.entity.OnlAuthRelation;
import com.csii.modules.online.auth.mapper.OnlAuthPageMapper;
import com.csii.modules.online.auth.mapper.OnlAuthRelationMapper;
import com.csii.modules.online.auth.service.IOnlAuthPageService;
import com.csii.modules.online.auth.vo.AuthColumnVO;
import com.csii.modules.online.auth.vo.AuthPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("onlAuthPageServiceImpl")
public class OnlAuthPageServiceImpl extends ServiceImpl<OnlAuthPageMapper, OnlAuthPage> implements IOnlAuthPageService {
	@Autowired
	private OnlAuthRelationMapper onlAuthRelationMapper;

	/**
	 * 修改
	 *
	 * @param authColumnVO
	 */
	public void disableAuthColumn(AuthColumnVO authColumnVO) {
		LambdaUpdateWrapper<OnlAuthPage> wrapper = Wrappers.lambdaUpdate(OnlAuthPage.class).eq(OnlAuthPage::getCgformId, authColumnVO.getCgformId())
				.eq(OnlAuthPage::getCode, authColumnVO.getCode()).eq(OnlAuthPage::getType, 1).set(OnlAuthPage::getStatus, 0);
		this.update(wrapper);
	}

	/**
	 * @param authColumnVO
	 */
	@Transactional
	public void enableAuthColumn(AuthColumnVO authColumnVO) {
		String id = authColumnVO.getCgformId();
		String code = authColumnVO.getCode();
		LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(OnlAuthPage.class).eq(OnlAuthPage::getCgformId, id).eq(OnlAuthPage::getCode, code).eq(OnlAuthPage::getType, 1);
		List volist = this.list(wrapper);
		if (volist != null && volist.size() > 0) {
			LambdaUpdateWrapper var6 = Wrappers.lambdaUpdate(OnlAuthPage.class).eq(OnlAuthPage::getCgformId, id)
					.eq(OnlAuthPage::getCode, code).eq(OnlAuthPage::getType, 1).set(OnlAuthPage::getStatus, 1);
			this.update(var6);
		} else {
			ArrayList list = new ArrayList();
			list.add(new OnlAuthPage(id, code, 3, 5));
			list.add(new OnlAuthPage(id, code, 5, 5));
			list.add(new OnlAuthPage(id, code, 5, 3));
			this.saveBatch(list);
		}

	}

	/**
	 * 选择项
	 *
	 * @param authColumnVO
	 */
	@Transactional
	public void switchAuthColumn(AuthColumnVO authColumnVO) {
		String id = authColumnVO.getCgformId();
		String code = authColumnVO.getCode();
		int flag = authColumnVO.getSwitchFlag();
		//
		if (flag == 1) {
			this.switchListShow(id, code, authColumnVO.a());
		} else if (flag == 2) {
			this.switchFormShow(id, code, authColumnVO.b());
		} else if (flag == 3) {
			this.switchFormEditable(id, code, authColumnVO.c());
		}

	}

	//
	@Transactional
	public void switchFormShow(String cgformId, String code, boolean flag) {
		this.switchFormShow(cgformId, code, 5, 5, flag);
	}

	@Transactional
	public void switchFormEditable(String cgformId, String code, boolean flag) {
		this.switchFormShow(cgformId, code, 3, 5, flag);
	}

	@Transactional
	public void switchListShow(String cgformId, String code, boolean flag) {
		this.switchFormShow(cgformId, code, 5, 3, flag);
	}

	public List<AuthPageVO> queryRoleAuthByFormId(String roleId, String cgformId, int type) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleAuthByFormId(roleId, cgformId, type);
	}

	public List<AuthPageVO> queryRoleDataAuth(String roleId, String cgformId) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleDataAuth(roleId, cgformId);
	}

	public List<AuthPageVO> queryAuthByFormId(String cgformId, int type) {
		return type == 1 ? ((OnlAuthPageMapper) this.baseMapper).queryAuthColumnByFormId(cgformId) : ((OnlAuthPageMapper) this.baseMapper).queryAuthButtonByFormId(cgformId);
	}

	public List<String> queryRoleNoAuthCode(String cgformId, Integer control, Integer page) {
		LoginUser var4 = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String var5 = var4.getId();
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(var5, cgformId, control, page, (Integer) null);
	}

	public List<String> queryFormDisabledCode(String cgformId) {
		return this.queryRoleNoAuthCode(cgformId, 3, 5);
	}

	public List<String> queryHideCode(String userId, String cgformId, boolean isList) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(userId, cgformId, 5, isList ? 3 : 5, (Integer) null);
	}

	public List<String> queryListHideColumn(String userId, String cgformId) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(userId, cgformId, 5, 3, 1);
	}

	public List<String> queryFormHideColumn(String userId, String cgformId) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(userId, cgformId, 5, 5, 1);
	}

	public List<String> queryFormHideButton(String userId, String cgformId) {
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(userId, cgformId, 5, 5, 2);
	}

	public List<String> queryHideCode(String cgformId, boolean isList) {
		LoginUser var3 = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String var4 = var3.getId();
		return ((OnlAuthPageMapper) this.baseMapper).queryRoleNoAuthCode(var4, cgformId, 5, isList ? 3 : 5, (Integer) null);
	}


	private void switchFormShow(String cgformId, String code, int control, int page, boolean flag) {
		LambdaQueryWrapper var6 = Wrappers.lambdaQuery(OnlAuthPage.class).eq(OnlAuthPage::getCgformId, cgformId).eq(OnlAuthPage::getCode, code)
				.eq(OnlAuthPage::getControl, control).eq(OnlAuthPage::getPage, page).eq(OnlAuthPage::getType, 1);
		OnlAuthPage onlAuthPage = (OnlAuthPage) ((OnlAuthPageMapper) this.baseMapper).selectOne(var6);
		if (flag) {
			if (onlAuthPage == null) {
				OnlAuthPage var8 = new OnlAuthPage();
				var8.setCgformId(cgformId);
				var8.setCode(code);
				var8.setControl(control);
				var8.setPage(page);
				var8.setStatus(1);
				((OnlAuthPageMapper) this.baseMapper).insert(var8);
			} else if (onlAuthPage.getStatus() == 0) {
				onlAuthPage.setStatus(1);
				((OnlAuthPageMapper) this.baseMapper).updateById(onlAuthPage);
			}
		} else if (!flag && onlAuthPage != null) {
			String var9 = onlAuthPage.getId();
			((OnlAuthPageMapper) this.baseMapper).deleteById(var9);
			this.onlAuthRelationMapper.delete(Wrappers.lambdaQuery(OnlAuthRelation.class).eq(OnlAuthRelation::getAuthId, var9));
		}

	}
}
